package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.clients.WmMediaClient;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.AuthDto;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.mapper.ApUserRealnameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* @author 慕颖
* @description 针对表【ap_user_realname(APP实名认证信息表)】的数据库操作Service实现
* @createDate 2022-09-12 08:21:43
*/
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
    implements ApUserRealnameService{

    @Autowired
    ApUserMapper apUserMapper;
    @Autowired
    WmMediaClient wmMediaClient;

    /**
     * 用户审核-查询列表
     * @param authDto
     */
    @Override
    public ResponseResult getList(AuthDto authDto) {
        // 1 设置分页参数
        Page<ApUserRealname> realnamePage = new Page<>(authDto.getPage(), authDto.getSize());
        // 2 包装条件
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        Page<ApUserRealname> pageInfo=new Page<>();
        if (!ObjectUtils.isEmpty(authDto.getStatus())) {
            wrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        } else {
            wrapper=null;//前端默认不传参【练空字符串都没有】
        }
        pageInfo = page(realnamePage, wrapper);
        PageResponseResult result = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) pageInfo.getTotal());
        result.setData(pageInfo.getRecords());
        return result;
    }

    /**
     * 审核失败 给出失败原因  （驳回操作）
     * 审核成功 注册媒体账号  （通过操作）
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult getStatus(AuthDto authDto,int type) {
        ApUserRealname byId = getById(authDto.getId());
        // status=2 审核失败
        if (type==2) {
            byId.setReason(authDto.getMsg());
            if (authDto.getMsg()==null) {
                authDto.setMsg(AppHttpCodeEnum.DATA_NOT_PASS.toString());
            }
            byId.setStatus(2);
            updateById(byId);
            return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_PASS);
        }
        // status=9 审核通过
        if (type==9) {
            byId.setStatus(9);
            updateById(byId);
            //审核通过，添加对应的媒体账号
            System.out.println("审核通过，注册自媒体账户");
            ApUser apUser = apUserMapper.selectById(byId.getUserId());
            wmMediaClient.addUser(apUser);
            return ResponseResult.okResult();
        }
        return ResponseResult.error(AppHttpCodeEnum.SERVER_ERROR);
    }

}




